package com.lemon.cloud.admin.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemon.cloud.admin.entity.SysFile;
import com.lemon.cloud.admin.service.SysFileService;
import com.lemon.cloud.admin.vo.FileVO;
import com.lemon.cloud.comm.constants.enums.LogBusType;
import com.lemon.cloud.comm.model.ResultMsg;
import com.lemon.cloud.log.annotation.SysLog;
import com.lemon.cloud.security.annotation.Inner;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/file")
@Tag(name = "一般文件接口")
public class SysFileController {

    @Autowired
    private SysFileService sysFileService;

    /**
     * 一般上传文件
     * @param file
     * @return
     */
    @Operation(
            summary = "一般上传文件",description = "一般上传文件",
            parameters = @Parameter(description = "上传的文件", required = true, schema = @Schema(type = "string", format = "binary")),
            responses = {
                    @ApiResponse(responseCode = "200",description = "successful operation",content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,schema = @Schema(implementation = FileVO.class))},useReturnTypeSchema = true),
                    @ApiResponse(responseCode = "400",description = "参数错误")
            }
    )
    @PostMapping(value = "/upload")
    public ResultMsg<FileVO> upload(@RequestPart("file") MultipartFile file) {
        return sysFileService.uploadFile(file);
    }

    /**
     * 获取文件
     * @param bucket 桶名称
     * @param fileName 文件空间/名称
     * @param response
     * @return
     */
    @Operation(
            summary = "获取文件",description = "上传文件",
            parameters = {
                    @Parameter(description = "桶名称", required = true,name = "bucket",in = ParameterIn.PATH),
                    @Parameter(description = "文件空间/名称", required = true, name = "fileName",in = ParameterIn.PATH)
            },
            responses = {
                    @ApiResponse(responseCode = "200",description = "successful operation"),
                    @ApiResponse(responseCode = "400",description = "参数错误")
            }
    )
    @Inner(false)
    @GetMapping("/{bucket}/{fileName}")
    public void file(@PathVariable("bucket") String bucket, @PathVariable("fileName") String fileName, HttpServletResponse response) {
        sysFileService.getFile(bucket, fileName, response);
    }

    /**
     * 分页查询
     * @param page
     * @param file
     * @return
     */
    @Operation(
            summary = "分页查询",description = "分页查询",
            responses = {
                    @ApiResponse(responseCode = "200",description = "successful operation")
            }
    )
    @GetMapping("/page")
    public ResultMsg<IPage<SysFile>> page(@ParameterObject Page page,
                                          @ParameterObject SysFile file){
        return ResultMsg.resultSuccess(sysFileService.page(page, Wrappers.query(file)));
    }


    /**
     * 详情
     * @param id
     * @return
     */
    @Operation(
            summary = "详情",description = "详情",
            parameters = @Parameter(name = "id",description = "id",required = true,in = ParameterIn.PATH),
            responses = {
                    @ApiResponse(responseCode = "200",description = "successful operation"),
                    @ApiResponse(responseCode = "400",description = "参数错误")
            }
    )
    @GetMapping("{id}")
    public ResultMsg<SysFile> getById(@PathVariable("id") Long id){
        return ResultMsg.resultSuccess(sysFileService.getById(id));
    }

    /**
     * 删除文件
     * @param id
     * @return
     */
    @Operation(
            summary = "删除文件",description = "删除文件",
            parameters = @Parameter(name = "id",description = "id",required = true,in = ParameterIn.PATH),
            responses = {
                    @ApiResponse(responseCode = "200",description = "successful operation")
            }
    )
    @DeleteMapping("{id}")
    @SysLog(value = "删除文件",businessType = LogBusType.DELETE)
    @PreAuthorize("hasAnyAuthority('sys_file_del')")
    public ResultMsg<Boolean> delFile(@PathVariable("id") Long id){
        return ResultMsg.resultSuccess(sysFileService.delFile(id));
    }

    /**
     * 获取文件外网的访问地址
     * @param bucket
     * @param fileName
     * @return
     */
    @Operation(
            summary = "获取文件外网的访问地址",
            parameters = {
                    @Parameter(name = "bucket",description = "桶名称",required = true,in = ParameterIn.PATH),
                    @Parameter(name = "fileName",description = "文件名称",required = true,in = ParameterIn.PATH),
            },
            responses = {
                    @ApiResponse(responseCode = "200",description = "successful operation"),
                    @ApiResponse(responseCode = "400",description = "参数错误")
            }
    )
    @Inner(false)
    @GetMapping("/online/{bucket}/{fileName}")
    public ResultMsg<String> onlineFile(@PathVariable("bucket") String bucket, @PathVariable("fileName") String fileName) {
        return ResultMsg.resultSuccess(sysFileService.onlineFile(bucket, fileName));
    }
}
